package net.chasing.androidbaseconfig.widget.dialog;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentTransaction;

import net.chasing.androidbaseconfig.R;

public class ProgressDialog extends BaseDialog {

    private String mMessage;
    private boolean isShowing;
    private ImageView mProgressView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mContentView = inflater.inflate(R.layout.view_loading, container, false);
        setDismissListener(() -> isShowing = false);
        return mContentView;
    }

    public boolean isShowing() {
        return isShowing;
    }

    @Override
    protected void getExtraParams() {
        Bundle bundle = getArguments();
        if (bundle == null)
            return;
        mMessage = bundle.getString("message");
    }

    public void setMessage(String text) {
        Bundle bundle = buildArguments();
        bundle.putString("message", text);
        setArguments(bundle);
    }

    @Override
    protected void initComponents(View view) {
        TextView tvMessage = view.findViewById(R.id.define_progress_tvMessage);
        if (!TextUtils.isEmpty(mMessage)) {
            tvMessage.setText(mMessage);
        }
        mProgressView = view.findViewById(R.id.define_progress);
        Animation rotateAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.rotate);
        LinearInterpolator lin = new LinearInterpolator();
        rotateAnimation.setInterpolator(lin);
        mProgressView.startAnimation(rotateAnimation);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (getDialog() != null) {
            WindowManager.LayoutParams params = getDialog().getWindow().getAttributes();
            params.gravity = Gravity.CENTER;
            params.dimAmount = 0f;
            getDialog().getWindow().setAttributes(params);

//            getDialog().getWindow().setBackgroundDrawableResource(R.color.transparent);
        }
    }

    @Override
    public void onDestroy() {
        if (mProgressView != null)
            mProgressView.clearAnimation();
        super.onDestroy();
    }

    @Override
    public int show(FragmentTransaction transaction, String tag) {
        if (isShowing) return 0;
        try {
            isShowing = true;
            return super.show(transaction, tag);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public void dismiss() {
        if (!isShowing) return;
        try {
            super.dismiss();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
